package org.ls.wxscancodes.Test;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.junit.Test;
import org.ls.wxscancodes.util.WechatConstants;
import org.ls.wxscancodes.util.httputil;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class ApiTest {
    private  String token="78_EyZWbquzxqJE2ilm779hjjzHs6hW_NXZ3DwybNxKJJ3NZ7NcxcFh4leqZLRB4-ijBY14qYI6_QH1NtMqZTJM-WG4E5erWr2JM9zCPgcdbjKxKKKizpFF-jNihmsZBHgAJACEK";
    /**
     * 获取token
     */
    @Test
    public  void  accessToken(){
        //https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
        Map<String, Object> param=new HashMap<>();
        param.put("grant_type","client_credential");
        param.put("appid", WechatConstants.APP_ID);
        param.put("secret", WechatConstants.APP_SECRECT);
        String json = httputil.doGet("https://api.weixin.qq.com/cgi-bin/token", param);
        JSONObject obj= JSONUtil.parseObj(json);
        System.out.println(obj.get("access_token"));
    }

    /**
     * 多环境
     * true就可以失效了
     */
    @Test
    public  void  stableToken(){
     String jsonStr="{\n" +
             "    \"grant_type\": \"client_credential\",\n" +
             "    \"appid\": \""+WechatConstants.APP_ID+"\",\n" +
             "    \"secret\": \""+WechatConstants.APP_SECRECT+"\",\n" +
             "    \"force_refresh\": false\n" +
             "} ";
        String json = httputil.doPost("https://api.weixin.qq.com/cgi-bin/stable_token", jsonStr,null);
        JSONObject obj= JSONUtil.parseObj(json);
        System.out.println(obj.get("access_token"));
    }

    @Test
    public void  qrcode(){
        String jsonStr="{\"expire_seconds\": 604800, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"login\"}}}";
        System.out.println(jsonStr);
        String json = httputil.doPost("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+token, jsonStr,null);
        JSONObject obj= JSONUtil.parseObj(json);
        System.out.println( obj.get("ticket"));
        System.out.println( obj.get("url"));
    }
}
